﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace CityByPro
{
    public partial class Default : System.Web.UI.Page
    {
        SqlConnection sqlcon;
        string strCon = ConfigurationManager.AppSettings["conn"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                //实例化链接对象
                SqlConnection SqlCon = new SqlConnection(strCon);
                string sqlstr = "select Province from tb_Province";
                SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
                //创建数据集
                DataSet myds = new DataSet();
                sqlcon.Open();
                //填充数据集
                myda.Fill(myds);
                //设置省份下拉框数据源
                DropDownList1.DataSource = myds;
                //设置项目Value值绑定的字段
                DropDownList1.DataValueField = "Province";
                //绑定数据
                DropDownList1.DataBind();
                //sql语句查询下拉框选择中省份下所有市
                string strCity = "select * from tb_City where Provincce=" + DropDownList1.SelectedItem.Text + "";
                SqlDataAdapter mydaCity = new SqlDataAdapter(strCity, sqlcon);
                DataSet mydsCity = new DataSet();
                mydaCity.Fill(mydsCity);
                DropDownList2.DataSource = mydsCity;
                DropDownList2.DataValueField = "City";
                DropDownList2.DataBind();
                sqlcon.Close();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "select * from tb_City where Province='" + DropDownList1.SelectedItem.Text + "'";
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds);
            DropDownList2.DataSource = myds;
            DropDownList2.DataValueField = "City";
            DropDownList2.DataBind();
            sqlcon.Close();
        }
    }
}